<?php
namespace Todo\Service;

/**
 * 访问控制
 * User: yepanpan
 * Date: 2016/8/8
 * Time: 17:08
 */
class AccessService extends BaseService
{
    private static $__INSTANCE = null;

    public static function getInstance(){
        if(!self::$__INSTANCE) self::$__INSTANCE = new AccessService();

        return self::$__INSTANCE;
    }

    /**
     * 添加权限
     * @param $title 资源名称
     * @param $bars 操作按钮
     */
    public function addAccess($title, $bars){
        $defaultUrl = '/'.MODULE_NAME .'/'. CONTROLLER_NAME.'/';
        $menuInfo = D('Todo/Sys/Menu')->where(array('url'=>$defaultUrl.'index'))->find();
        if(!$menuInfo) return false;

        //默认子页面
        $defaultPage = array('index'=>$title.'列表', 'edit'=>'编辑'.$title, 'add'=>'新增'.$title);
        //默认接口
        $defaultApi = array('lists'=>$title.'查询接口', 'update'=>$title.'更新接口', 'insert'=>$title.'新增接口',
            'edit'=>$title.'更新接口', 'add'=>$title.'新增接口', 'delete'=>$title.'删除接口', 'info'=>$title.'详细接口');
        //公开接口
        $publicApi = array('format'=>$title.'格式化JS');
        //页面需要的接口
        foreach ($bars as $bar){
            if($bar == 'enable') $defaultApi[$bar] = $title.'启用接口';
            if($bar == 'disable') $defaultApi[$bar] = $title.'禁用接口';
            if($bar == 'exorts') $defaultApi[$bar] = $title.'导出接口';
            if($bar == 'imports') {
                $defaultApi[$bar] = $title.'导入接口';
                $defaultPage[$bar] = $title.'导入页面';
            }
            if($bar == 'view') {
                $defaultPage[$bar] =  $title.'查看页面';
            }
            if($bar == 'verify') {
                $defaultApi[$bar] =  $title.'审核接口';
                $defaultPage[$bar] =  $title.'审核页面';
            }
        }

        //添加页面和接口
        foreach ($defaultPage as $url=>$title){
            $this->_addAccess($title, MODULE_NAME .'/'. CONTROLLER_NAME.'/'.$url, 2, 2, $menuInfo['id']);
        }
        foreach ($defaultApi as $url=>$title){
            $this->_addAccess($title, MODULE_NAME .'/'. CONTROLLER_NAME.'/'.$url, 1, 2, $menuInfo['id']);
        }
        foreach ($publicApi as $url=>$title){
            $this->_addAccess($title, MODULE_NAME .'/'. CONTROLLER_NAME.'/'.$url, 1, 1, $menuInfo['id']);
        }

        //非Index管理页面
        if(ACTION_NAME != 'index'){
            $menuInfo = D('Todo/Sys/Menu')->where(array('url'=>$defaultUrl.ACTION_NAME))->find();
            if(!$menuInfo) return false;

            $this->_addAccess($title, MODULE_NAME .'/'. CONTROLLER_NAME.'/'.ACTION_NAME, 2, 2, $menuInfo['id']);
            $this->_addAccess($title, MODULE_NAME .'/'. CONTROLLER_NAME.'/view', 2, 2, $menuInfo['id']);
        }
    }

    protected function _addAccess($title, $url, $type, $level, $menuId){
        $am = M('sys_access');
        $info = $am->where(array('url'=>$url))->find();
        if($info) $accessId = $info['id'];
        else $accessId = $am->add(array('title'=>$title,'url'=>$url, 'type'=>$type,'level'=>$level));

        $mcm = M('sys_menu_access');
        $data = array('access_id'=>$accessId, 'menu_id'=>$menuId);
        $info = $mcm->where($data)->find();
        if($info) return true;

        return $mcm->add($data);
    }
}